package il.ac.tau.cg.ex3.entities.styles;

public class Color
{
	private double r;
	private double g;
	private double b;
	
	public Color()
	{
		set(1.0, 1.0, 1.0);
	}
	
	public Color(double r, double g, double b)
	{
		set(r, g, b);
	}
	
	public Color(int color)
	{
		set(color);
	}
	
	public void set(double r, double g, double b)
	{
		this.r = r;
		this.g = g;
		this.b = b;
	}
	
	public void set(int color)
	{
		double r = ((color >> 16) & 0xFF) / 255.0;
		double g = ((color >> 8) & 0xFF) / 255.0;
		double b = (color & 0xFF) / 255.0;
		
		set(r, g, b);
	}
	
	@Override
	public Color clone()
	{
		return new Color(this.r, this.g, this.b);
	}
	
	public double getR()
	{
		return r;
	}
	
	public int getIntR()
	{
		return denormalize(r);
	}
	
	public void setR(double r)
	{
		this.r = r;
	}
	
	public double getG()
	{
		return g;
	}
	
	public int getIntG()
	{
		return denormalize(g);
	}
	
	public void setG(double g)
	{
		this.g = g;
	}
	
	public double getB()
	{
		return b;
	}
	
	public int getIntB()
	{
		return denormalize(b);
	}
	
	public int getColor()
	{
		return (getIntR() << 16) | (getIntG() << 8) | getIntB();
	}
	
	public void setB(double b)
	{
		this.b = b;
	}
	
	public double normalize(int component)
	{
		return (component / 255.0);
	}
	
	public int denormalize(double component)
	{
		return (int)(component * 255);
	}
	
	public Color add(Color other)
	{
		return new Color(r + other.r, g + other.g, b + other.b);
	}
	
	public Color mul(Color other)
	{
		return new Color(r * other.r, g * other.g, b * other.b);
	}
	
	public Color scale(double s)
	{
		return new Color(s * r, s * g, s * b);
	}
	
	public Color trim()
	{
		double cr = Math.max(Math.min(r, 1.0), 0.0);
		double cg = Math.max(Math.min(g, 1.0), 0.0);
		double cb = Math.max(Math.min(b, 1.0), 0.0);
		
		return new Color(cr, cg, cb);
	}
	
	@Override
	public String toString()
	{
		return "(" + r + ", " + g + ", " + b + ")";
	}
}

